/*------------------------------------------------------*/
/*                                                      */
/* Copyright (C)	sky-mobi.com                        */
/* All rights reserved                                  */
/*                                                      */
/* Author   tristan.yang                                */
/*                                                      */
/* History  2011-5-20 created                           */
/*                                                      */
/*------------------------------------------------------*/

#ifndef __ME_CONNECT_H__
#define __ME_CONNECT_H__

#include "RtBase.h"
#include "RtTimeValue.h"
#include "RtUtilTemplates.h"
#include "connprovider.h"
#include "tsbase.h"

class CMeConnectSink
{
public:
	// report source update info
	virtual void OnReceiveMeMsg(CRtMessageBlock& aData, LPVOID pconn) = 0;
	virtual void OnDisconnect(RtResult aReason, LPVOID pconn) = 0;
	virtual ~CMeConnectSink() {}
};

class CMeConnect
: public CRtTimerWrapperSink
, public IConnectionSink
, public CRtReferenceControlSingleThread
{
public:
	CMeConnect(CMeConnectSink* psink);
	virtual ~CMeConnect();
	virtual RtResult SetConnection(IConnection *pConnection);
	
	RtResult SendPdu(CRtMessageBlock& inMessage);

protected: // from CRtTimerWrapperSink
	virtual void OnTimer(CRtTimerWrapper* aID);
	
protected: // from IConnectionSink
	virtual void OnReceiveData(CRtMessageBlock& aData, IConnection* pConn);
	virtual void OnDisconnect(RtResult aReason, IConnection* pConn); 
	virtual void OnSend(IConnection* pConn);
	
private:
	RtResult TrySendMessages();

private:
	std::list<CRtMessageBlock*>	m_sendmsgs;
	BOOL						m_bCanSend;

	CRtAutoPtr<IConnection> m_pConnect;
	ChannelVec				m_SubLiveChannels;
	CMeConnectSink*			m_pSink;
};

#endif//__ME_CONNECT_H__
